package com.ff.xyh.service.complaint.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.qo.complaint.ComplaintHandleQuery;
import com.ff.xyh.common.entity.complaint.ComplaintHandle;
import com.ff.xyh.service.complaint.mapper.ComplaintHandleMapper;
import com.ff.xyh.service.complaint.service.ComplaintHandleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ff
 * @since 2023-05-24
 */
@Service
public class ComplaintHandleServiceImpl extends ServiceImpl<ComplaintHandleMapper, ComplaintHandle> implements ComplaintHandleService {

    @Override
    public ResultObject getComplaintHandleCondition(ComplaintHandleQuery complaintHandleQuery) {

        Page<ComplaintHandle> page = new Page<>(complaintHandleQuery.getCurrentPage(), complaintHandleQuery.getPageSize());
        List<ComplaintHandle> resultList = new ArrayList<>();
        QueryWrapper<ComplaintHandle> wrapper = new QueryWrapper<>();


        if (complaintHandleQuery.getGmtAfter() != null) {
            wrapper.ge("gmt_created", complaintHandleQuery.getGmtAfter());
        }
        if (complaintHandleQuery.getGmtBefore() != null) {
            wrapper.le("gmt_created", complaintHandleQuery.getGmtBefore());
        }
        // 按创建时间排序意味着要最新
        if (complaintHandleQuery.getOrderByCreatedTime() != null && complaintHandleQuery.getOrderByCreatedTime()) {
            // 降序
            wrapper.orderByDesc("gmt_created");
        } else {
            wrapper.orderByAsc("gmt_created");
        }


        if (!StringUtils.isEmpty(complaintHandleQuery.getComplaintId())) {
            wrapper.eq("complaint_id", complaintHandleQuery.getComplaintId());
        }
        if (!StringUtils.isEmpty(complaintHandleQuery.getOperatorId())) {
            wrapper.eq("operator_id", complaintHandleQuery.getOperatorId());
        }
        if (complaintHandleQuery.getStatus() != null) {
            wrapper.eq("status", complaintHandleQuery.getStatus());
        }

        if (!StringUtils.isEmpty(complaintHandleQuery.getTextContain())) {
            wrapper.like("handle_text", complaintHandleQuery.getTextContain());
        }

        IPage<ComplaintHandle> resultPage = this.page(page, wrapper);

        return ResultObject.ok().putList(resultPage.getRecords(), resultPage.getTotal());
    }
}
